import java.util.HashMap;

/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution560 {

    public static void main(String args[]) {
        Solution560 s = new Solution560();
        System.out.println(s.subarraySum(new int[]{1, 1, 1, 1}, 4));
    }

    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> mp = new HashMap<>();
        int pre[] = new int[nums.length + 1];

        int cnt = 0;
        mp.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            pre[i + 1] = pre[i] + nums[i];
            if (mp.containsKey(pre[i + 1] - k)) {
                cnt += mp.get(pre[i + 1] - k);
            }
            mp.put(pre[i + 1], mp.containsKey(pre[i + 1]) ? mp.get(pre[i + 1]) + 1 : 1);
        }
        return cnt;
    }

}
